home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / system / exelink2.zip / EXELINK.ASM next >
Assembly Source File  |  1993-03-03  |  6KB  |  187 lines

  1. ; exelink.asm
  2. ; Copyright (c) 1993 Stephen Harris
  3. ;
  4. ; A TSR program which intercepts all EXEC calls (int21 function 4Bh).
  5. ; If the call is to run a program residing in the directory listed in
  6. ; 'linknm' then instead of executing that it will open the file and read in
  7. ; the name of a program, and execute that instead.
  8. ;
  9. ; see exelink.doc for details
  10. ;
  11. ; This is version 2, which releases unused memory (environment block) and
  12. ; relocates itself over the PSP to make the resident part as small as
  13. ; possible.
  14. ;
  15. ; Written for the TINY model in Turbo Assembler.  Should work for MASM
  16. ; since Borland say that TASM is MASM compatible...
  17. ;
  18. ; For Turbo Assembler use:
  19. ;   tasm exelink
  20. ;   tlink /t exelink
  21. ; For MASM use:
  22. ;   masm exelink;
  23. ;   link exelink;
  24. ;   exe2bin exelink.exe exelink.com
  25. ; NOTE:  The MASM stuff is just a guess - I don't use it!
  26.  
  27. cseg            segment para    public  'code'
  28.                 assume cs:cseg, ds:cseg, es:cseg
  29.  
  30.                 org     100h
  31.  
  32. START:          jmp     setup
  33.  
  34. oint21          dd      ?
  35.  
  36. linknm          db      'C:\LINKS\'
  37. len_link        equ     9
  38.  
  39. execnm          db      128 dup (0)
  40.  
  41. handle          dw      0
  42.  
  43. nint21          proc    far
  44.                 pushf
  45.                 cmp     ah,4bh
  46.                 jne     exit2
  47.  
  48. exec:           push    ax                      ;       Save everything
  49.                 push    bx
  50.                 push    cx
  51.                 push    es
  52.                 push    si
  53.                 push    di
  54.                 push    bp
  55.                 push    dx
  56.                 push    ds
  57.  
  58.                 mov     si,dx
  59.                 push    cs
  60.                 pop     es
  61.                 mov     di,offset execnm
  62.                 mov     ah,60h
  63.                 int     21h
  64.  
  65.                 push    cs
  66.                 pop     ds
  67.                 mov     si,offset linknm        ; ds:si point to linknm
  68.  
  69.                 push    cs
  70.                 pop     es
  71.                 mov     di,offset execnm        ;es:di point to execnm
  72.  
  73.                 mov     cx,len_link
  74.                 cld
  75.                 repe    cmpsb                   ; check if identical
  76.  
  77.                 jne     exit                    ; nope - exit as normal
  78.  
  79.                 ; oh gosh!  the same!  Open the file and find what the link
  80.                 ; points to
  81.  
  82.                 mov     dx,offset execnm        ; open it
  83.                 mov     ax,3d00h
  84.                 int     21h
  85.                 jc      exit                    ; if can not open, exit normal
  86.                 mov     cs:handle,ax
  87.  
  88.                 mov     bx,ax                   ; get the name from it
  89.                 mov     cx,127
  90.                 mov     ah,3fh
  91.                 int     21h
  92.  
  93.                 mov     ah,3eh                  ; close it
  94.                 mov     bx,cs:handle
  95.                 int     21h
  96.  
  97.                 mov     di,offset execnm-1      ; turn to ASCIIZ
  98. lp:             inc     di
  99.                 mov     al,byte ptr cs:[di]
  100.                 cmp     al,32
  101.                 jg      lp
  102.                 mov     byte ptr cs:[di],0
  103.  
  104.                 pop     ds                      ; throw away old exec name
  105.                 pop     dx
  106.  
  107.                 push    cs                      ; set pointers to new name
  108.                 pop     ds
  109.                 mov     dx,offset execnm
  110.                 jmp     exit1b
  111.  
  112. exit:           pop     ds                      ; restore registers
  113.                 pop     dx
  114. exit1b:         pop     bp
  115.                 pop     di
  116.                 pop     si
  117.                 pop     es
  118.                 pop     cx
  119.                 pop     bx
  120.                 pop     ax
  121.  
  122. exit2:          popf
  123.                 jmp     cs:[oint21]
  124.  
  125. nint21          endp
  126.  
  127. ; there ended the TSR code.  The next stuff is simple initialisation.
  128. ; if gets the old int21 vector, throws away the environment, then
  129. ; relocates itself down to 60h in the PSP.  Since we don't use FCB's in
  130. ; this code, there will never be a need for the DTA - even assuming a TSR
  131. ; uses its own DTA!
  132.  
  133. setup:          xor     ax,ax                   ;       Get old 21h vector
  134.                 mov     es,ax
  135.                 mov     ax,3521h
  136.                 int     21h
  137.                 mov     word ptr oint21,bx
  138.                 mov     word ptr oint21+2,es
  139.  
  140.                 push    cs
  141.                 pop     ds
  142.  
  143.                 mov     es,ds:[2ch]             ;       lose environment
  144.                 mov     ah,49h
  145.                 int     21h
  146.  
  147.                 cld                             ;       move tsr over PSP
  148.                 mov     si,100h
  149.                 mov     di,60h
  150.                 push    ds
  151.                 pop     es
  152.                 mov     cx,setup-START
  153.                 rep     movsb
  154.  
  155.                 mov     ax,ds                   ;       work out new segment
  156.                 sub     ax,0ah                  ;       for relocated code
  157.                 mov     ds,ax
  158.  
  159.                 mov     dx,offset nint21        ;       set new int21 address
  160.                 mov     ax,2521h
  161.                 int     21h
  162.  
  163.                 mov     ah,9                    ;       Sign on message
  164.                 mov     dx,offset ident
  165.                 push    cs
  166.                 pop     ds
  167.                 int     21h
  168.  
  169. ; Now we need to go TSR.  Calculate number of paragraphs code needs
  170. IF              (setup-START) mod 16
  171. ROUNDUP         =       1
  172. ELSE
  173. ROUNDUP         =       0
  174. ENDIF
  175.  
  176.                 mov     dx,((setup-START)+60h)/16+ROUNDUP
  177.  
  178.                 mov     ax,3100h                ;       TSR
  179.                 int     21h
  180.  
  181. ident:          db      'Command file linking v2. '
  182.                 db      'Copyright (c) 1993 Stephen Harris',13,10
  183.                 db      '$'
  184.  
  185. cseg            ends
  186.                 end     START
  187.